package persistence;

import org.hibernate.Session;

public class ObjetoDAO {

	public void save(Object objeto) {
			
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		try {
			session.getTransaction().begin();
		    session.merge(objeto);
		    session.getTransaction().commit();
		} catch (Exception exp) {
			 session.getTransaction().rollback();
			 System.out.println(exp.toString());
		} finally{
			session.close();
		}
		
	}
	
	@SuppressWarnings("rawtypes")
	public Object recupera(Class classe, Long id){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		Object valor = session.get(classe, id);
		
		return valor;
	}
	
	public void excluir(Object objeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		try {
			session.getTransaction().begin();
			session.delete(objeto);
		    session.getTransaction().commit();
		} catch (Exception exp) {
			 session.getTransaction().rollback();
			 System.out.println(exp.toString());
		} finally{
			session.close();
		}
	}
}
